import React, {Component} from 'react';
// import MyEditor from "../components/Myeditor";
import {Button} from "antd";
import axios from "axios";

export default class Upload extends Component {
    constructor(props) {
        super();
        this.state = ({
            saveFileName: null,
        })
    }

    goUpload = () => {
        let file = document.querySelector('input[type=file]').files[0];
        //if(!app.verifyFile(file)){
        //    return;
        //}

        let formData = new window.FormData();
        formData.append('file', file);
        console.log(formData);
        axios({
            method: "POST",
            url: "http://localhost:8090/knowledge/upload",
            headers: {
                'Content-Type': 'multipart/form-data' //注
            },
            data: formData
        }).then((res) => {
            //这里返回保存的文件名
            console.log(res.data);
            const knowledge={
                title:document.getElementById("title").value,
                detail:res.data.detail,
                detailHtml:res.data.detailHtml,
                editTime: Date.parse(new Date()),
// todo 根据数据库获取
                userId:1,
                userState:0,
                createTime: Date.parse(new Date()),
                visitors:0,
                parentId:1,
            }
            console.log(knowledge)
            axios.post("http://localhost:8090/knowledge/insertIntoKnowledge",knowledge);
        }).catch((err) => {
            console.log(err)
        });
    }

    render() {
        return (
            <>
                <>
                    <h1>文档上传</h1>
                    <input type="file"/>
                    <Button onClick={() => this.goUpload()}>上传</Button>

                </>
                {/*<MyEditor/>*/}
            </>
        )
    }
}